package com.wyx.jdbc.PrepareStatement_;

import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class Exercise01 {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        Properties properties = new Properties();
        properties.load(new FileReader("src\\mysql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        /*//添加数据
        String sql = "insert into admin values(?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        System.out.print("请输入管理员名称:");
        String admin_name = scanner.nextLine();
        System.out.print("请输入密码:");
        String admin_pwd = scanner.nextLine();
        preparedStatement.setString(1,admin_name);
        preparedStatement.setString(2,admin_pwd);*/

        /*//修改tom的记录，将name改为king
        String sql = "update admin set name = ? where name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        System.out.print("请输入你要修改的管理员名称:");
        String admin_name = scanner.nextLine();
        System.out.print("请输入新名字");
        String admin_newName = scanner.nextLine();
        preparedStatement.setString(1,admin_newName);
        preparedStatement.setString(2,admin_name);*/


        /*//删除jack的记录
        String sql = "delete from admin where name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        System.out.print("请输入你要删除的管理员名称:");
        String admin_name = scanner.nextLine();
        preparedStatement.setString(1,admin_name);*/


       /* int i = preparedStatement.executeUpdate();
        System.out.println(i > 0 ? "成功" : "失败");*/


        //查询admin表的全部记录
        String sql = "select * from admin";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            String name = resultSet.getString("name");
            String pwd = resultSet.getString("pwd");
            System.out.println("name:" + name + "  pwd:" +pwd);
        }

        resultSet.close();
        preparedStatement.close();
        connection.close();

    }
}
